<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 * @version		$Id: Mailer.php 108 2011-04-11 17:33:46Z ninhgio@gmail.com $
 */

class Modules_Mail_Services_Mailer
{
	/**
	 * Mail transport instance
	 * 
	 * @var Zend_Mail_Transport
	 */
	private static $_transport = null;
	
	/**
	 * Get mail transport
	 * 
	 * @return Zend_Mail_Transport
	 */
	public static function getMailTransport()
	{
		if (self::$_transport == null) {
			require_once LIB_DIR . DS . 'phpmailer' . DS . 'class.phpmailer.php'; 
			require_once LIB_DIR . DS . 'phpmailer' . DS . 'class.smtp.php'; 
			$config = Gio_Core_Module::getConfig('mail', true);
			self::$_transport = new PHPMailer();
			self::$_transport->CharSet = 'utf-8';
			
			switch ($config['protocol']['protocol']) {
				case 'mail':
					self::$_transport->Mailer = 'mail';
					break;				
				case 'smtp':
					self::$_transport->Mailer = 'smtp';
					$options = array();
					/**
					 * Check host setting
					 */
					if (isset($config['smtp']['host'])) {
						$options['host'] = $config['smtp']['host'];
					}
					
					/**
					 * Check port setting
					 */
					if (isset($config['smtp']['port'])) {
						$options['port'] = $config['smtp']['port'];
					}
					
					/**
					 * Check authentication settings
					 */
					if (isset($config['smtp']['username']) && isset($config['smtp']['password'])) {
						$options['auth'] 	 = 'login';
						$options['username'] = $config['smtp']['username'];
						$options['password'] = $config['smtp']['password'];
					}
					
					/**
					 * Check security setting
					 */
					if (isset($config['smtp']['security'])) {
						$options['ssl'] = $config['smtp']['security'];
					}
					
					self::$_transport->Host = $options['host']; // specify main and backup server
					self::$_transport->Port = $options['port']; // set the port to use
					self::$_transport->SMTPAuth = true; // turn on SMTP authentication
					self::$_transport->SMTPSecure = $options['ssl'];
					self::$_transport->Username = $options['username']; // your SMTP username or your gmail username
					self::$_transport->Password = $options['password']; // your SMTP password or your gmail password
					break;
			}
		}
		
		return self::$_transport;
	}
	
	/**
	 * Send email to all users who have the same role
	 * 
	 * @param Core_Models_Role $role
	 * @param Mail_Models_Template $template
	 */
	public static function sendToRole($role, $template)
	{
		$criteria  = ($role->role_id == '') 
						? array('status' => 1) 
						: array('role' => $role->role_id, 'status' => 1);
		$users 	   = Core_Services_User::find(null, null, $criteria);
		
		/**
		 * Get mail transport instance
		 */
		$transport = self::getMailTransport();
		
		/**
		 * Send email to each user
		 * @see http://framework.zend.com/manual/en/zend.mail.multiple-emails.html
		 */
		$search    = array(Mail_Models_Mail::MAIL_VARIABLE_EMAIL, Mail_Models_Mail::MAIL_VARIABLE_USERNAME);
		
		foreach ($users as $u) {
			$replace = array($u->email, $u->user_name);
			$subject = str_replace($search, $replace, $template->subject);
			$content = str_replace($search, $replace, $template->content);
			
			$mail 	 = new Mail_Models_Mail(array(
											'template_id' 	  => $template->template_id,
											'subject' 		  => $subject,
											'content' 		  => $content,
											'created_user_id' => $user->user_id,
											'from_mail' 	  => $template->from_mail,
											'from_name' 	  => $template->from_name,
											'reply_to_mail'   => $template->reply_to_mail,
											'reply_to_name'   => $template->reply_to_name,
											'to_mail' 		  => $u->email,
											'to_name'         => $u->user_name,
											'status' 		  => 'sent',
											'created_date'    => date('Y-m-d H:i:s'),
											'sent_date' 	  => date('Y-m-d H:i:s'),
										));
			Mail_Services_Mail::add($mail);
			
			$sender = new Zend_Mail();
			$sender->clearFrom()
				   ->setFrom($template->from_mail, $template->from_name)
				   ->clearReplyTo()
				   ->setReplyTo($template->reply_to_mail, $template->reply_to_name)
				   ->addTo($u->email, $u->user_name)
				   ->setSubject($subject)
				   ->setBodyHtml($content)
				   ->send($transport);
		}
	}
}
